package com.itfreer.print.toPdf;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory;

/**
 * 定义将ppt转换为image图片接口
 */
public class PptToImageImp {
	
	/**
	 * 将ppt转换为image图片
	 * @param pptFile
	 * @return
	 * @throws EncryptedDocumentException
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<ByteArrayOutputStream> toImages(InputStream pptFile) throws EncryptedDocumentException, IOException {
		List<ByteArrayOutputStream> al = new ArrayList<ByteArrayOutputStream>();
		SlideShow ppt = SlideShowFactory.create(pptFile);
		pptFile.close();
		Dimension pgsize = ppt.getPageSize();
		List<Slide> slide = ppt.getSlides();
		for (int i = 0; i < slide.size(); i++) {
			BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
			Graphics2D graphics = img.createGraphics();
			graphics.setPaint(Color.white);
			graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
			Slide slideItem = slide.get(i);
			slideItem.draw(graphics);
			
			ByteArrayOutputStream imageFile = new ByteArrayOutputStream();
			javax.imageio.ImageIO.write(img, "jpeg", imageFile);
			imageFile.close();
			al.add(imageFile);
		}
		return al;
	}
}
